package LeetCode1;

/**
 * 两数相加
 *
 * @author 是阿秋啊
 * @date 2022/03/28 18:41
 **/
public class Num2 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(2);
        ListNode la = new ListNode(4);
        ListNode lb = new ListNode(3);
        l1.next = la;
        la.next = lb;
        ListNode l2 = new ListNode(5);
        ListNode lc = new ListNode(6);
        ListNode ld = new ListNode(4);
        l2.next = lc;
        lc.next = ld;
        ListNode head = addTwoNumbers(l1, l2);
    }

    // 自己写的
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode sumHead = new ListNode(-1);
        // 判空
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        // 保存l1 + l2 > 10 的部分
        int temp = 0;
        while (l1 != null && l2 != null) {
            int sum = l1.val + l2.val + temp;
            temp = sum / 10;
            int value = sum % 10;
            ListNode node = new ListNode(value);
            ListNode prev = sumHead;
            while (prev.next != null) {
                prev = prev.next;
            }
            prev.next = node;
            l1 = l1.next;
            l2 = l2.next;
        }
        if (l1 == null && l2 == null) {
            // 此时表示两个链表一样长
            if (temp != 0) {
                // 虽然链表走空，但是temp不为空
                ListNode prev = sumHead;
                ListNode node = new ListNode(temp);
                while (prev.next != null) {
                    prev = prev.next;
                }
                prev.next = node;
            }
        } else if (l1 == null) {
            // l2未空
            ListNode prev = sumHead;
            while (l2 != null) {
                int sum = l2.val + temp;
                temp = sum / 10;
                int value = sum % 10;
                ListNode node = new ListNode(value);
                while (prev.next != null) {
                    prev = prev.next;
                }
                prev.next = node;
                l2 = l2.next;
            }
            // 判断temp是否还有进位
            if (temp != 0) {
                ListNode node = new ListNode(temp);
                while (prev.next != null) {
                    prev = prev.next;
                }
                prev.next = node;
            }
        } else {
            // l1未空
            ListNode prev = sumHead;
            while (l1 != null) {
                int sum = l1.val + temp;
                temp = sum / 10;
                int value = sum % 10;
                ListNode node = new ListNode(value);
                while (prev.next != null) {
                    prev = prev.next;
                }
                prev.next = node;
                l1 = l1.next;
            }
            // 判断temp是否还有进位
            if (temp != 0) {
                ListNode node = new ListNode(temp);
                while (prev.next != null) {
                    prev = prev.next;
                }
                prev.next = node;
            }
        }
        return sumHead.next;
    }

    // 官方答案,答案比自己写的最大优化在于巧用了三元式判断
//    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//        ListNode dummyHead = new ListNode(-1);
//        ListNode prev = dummyHead;
//        int temp = 0;
//        while (l1 != null || l2 != null) {
//            int x = l1 == null ? 0 : l1.val;
//            int y = l2 == null ? 0 : l2.val;
//            int sum = x + y + temp;
//            temp = sum / 10;
//            int value = sum % 10;
//            ListNode node = new ListNode(value);
//            prev.next = node;
//            prev = prev.next;
//            if (l1 != null) {
//                l1 = l1.next;
//            }
//            if (l2 != null) {
//                l2 = l2.next;
//            }
//        }
//        if (temp != 0) {
//            prev.next = new ListNode(temp);
//        }
//        return dummyHead.next;
//    }
}